﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using System;

namespace dnp.Framework.Web
{

    /// <summary>
    /// Parses a given string representation of a URL into its components and optionally reassembles these components back to a string representation.
    /// </summary>
    public class Url : Uri
    {
        public Url(string uriString)
            : base(uriString, true)
        {
        }

        public Url(string uriString, bool stripDefaultHost)
            : base(Initialize(uriString, stripDefaultHost))
        {
        }

        private static string Initialize(string uriString, bool stripDefaultHost)
        {
            if (stripDefaultHost)
                if (uriString.Contains("www."))
                    uriString = uriString.Replace("www.", "");

            return uriString;
        }

        /// <summary>
        /// Returns the sub-domain for the host if present
        /// </summary>
        public string SubDomain
        {
            get
            {
                string[] hostParts = this.Host.Split('.');
                if (hostParts.Length > 2)
                    return hostParts[0];

                return "";
            }
        }

        /// <summary>
        /// Returns the full path after the host name
        /// </summary>
        public string Path
        {
            get { return this.PathAndQuery; }
        }

        public string HostRoot
        {
            get
            {
                if (!string.IsNullOrEmpty(this.SubDomain))
                    return this.Host.Substring(this.SubDomain.Length + 1);
                else
                    return this.Host;
            }
        }
    }
}

